package com.yyhh.helpcall.activity.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.yyhh.helpcall.R;
import com.yyhh.helpcall.model.RecordModel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class TestRecordsAdapter extends RecyclerView.Adapter<TestRecordsAdapter.RecordsViewHolder> {

    public ArrayList<RecordModel> datas = new ArrayList<>();

    public View.OnClickListener onClickListener;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public void setOnItemClickListener(View.OnClickListener listener){
        onClickListener = listener;
    }

    @NonNull
    @Override
    public TestRecordsAdapter.RecordsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.records_item_layout,parent,false);
        return new TestRecordsAdapter.RecordsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TestRecordsAdapter.RecordsViewHolder holder, int position) {
        RecordModel bean = datas.get(position);
        holder.phone.setText(bean.name);
        holder.name.setText("路径:" + bean.path);
        holder.time.setText("时间戳:" + bean.lastModifyTime);
        holder.sex.setText("格式化后的时间:" + simpleDateFormat.format(new Date(bean.lastModifyTime)));
        holder.callButton.setVisibility(View.GONE);
        if(onClickListener != null){
            holder.callButton.setTag(bean);
            holder.callButton.setOnClickListener(onClickListener);
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public static final class RecordsViewHolder extends RecyclerView.ViewHolder{

        public TextView phone,name,time,sex;
        public ImageView callButton;
        public RecordsViewHolder(@NonNull View itemView) {
            super(itemView);
            phone = itemView.findViewById(R.id.phone);
            name = itemView.findViewById(R.id.name);
            time = itemView.findViewById(R.id.time);
            sex = itemView.findViewById(R.id.sex);
            callButton = itemView.findViewById(R.id.call_button);
        }
    }
}
